﻿using ApplicationGUI.Properties;
using Metabase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ApplicationGUI
{
    public partial class RepoPropertyForm : Form
    {
        public string ServerName;
        public string DataBase;
        public string User;
        public string Password;
        public bool DomainAuth;
        public string RepoName;

        public RepoPropertyForm()
        {
            InitializeComponent();
        }

        private void checkBoxDomainAuth_CheckedChanged(object sender, EventArgs e)
        {
            textBoxUser.Enabled = !checkBoxDomainAuth.Checked;
            textBoxPassword.Enabled = !checkBoxDomainAuth.Checked;
            if (checkBoxDomainAuth.Checked)
            {
                textBoxUser.Text = "";
                textBoxPassword.Text = "";
            }
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            ServerName = textBoxServer.Text;
            DataBase = textBoxBase.Text;
            User = textBoxUser.Text;
            Password = textBoxPassword.Text;
            DomainAuth = checkBoxDomainAuth.Checked;
            RepoName = textBoxName.Text;
        }

        private void RepoPropertyForm_Shown(object sender, EventArgs e)
        {
            textBoxServer.Text = ServerName;
            textBoxBase.Text = DataBase;
            textBoxUser.Text = User;
            textBoxPassword.Text = Password;
            checkBoxDomainAuth.Checked = DomainAuth;
            textBoxName.Text = RepoName;
        }

        private void Connection_button_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            DBHandler dbHandler = new DBHandler { ServerName = textBoxServer.Text, Database = textBoxBase.Text, UserName = textBoxUser.Text, Pwd = textBoxPassword.Text, DomainAuth = checkBoxDomainAuth.Checked, Schema = "dbo" };
            bool connResult = dbHandler.Connect();
            if (!connResult)
            {
                MessageBox.Show(dbHandler.connectException.Message, AppResources.ErrorHeader, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show(AppResources.ConnectOkInformation, AppResources.InfoHeader, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            Cursor.Current = Cursors.Default;
        }
    }
}
